package com.yxdj.feedback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxdj.common.core.constant.SecurityConstants;
import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.exception.ServiceException;
import com.yxdj.feedback.api.domain.ReviewImage;
import com.yxdj.feedback.api.domain.Tag;
import com.yxdj.feedback.api.domain.UserReview;
import com.yxdj.feedback.api.dto.UserReviewDto;
import com.yxdj.feedback.api.vo.UserReviewVo;
import com.yxdj.feedback.mapper.ReviewImageMapper;
import com.yxdj.feedback.mapper.TagMapper;
import com.yxdj.feedback.mapper.UserReviewMapper;
import com.yxdj.feedback.service.UserReviewService;
import com.yxdj.order.api.RemoteOrderService;
import com.yxdj.order.api.domain.OrderInfo;
import com.yxdj.user.api.RemoteManageUserService;
import com.yxdj.user.api.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
* @author 韩总
* @description 针对表【user_review(用户评价表)】的数据库操作Service实现
* @createDate 2025-03-20 14:44:40
*/
@Service
public class UserReviewServiceImpl extends ServiceImpl<UserReviewMapper, UserReview>
    implements UserReviewService {

    @Autowired
    private RemoteManageUserService remoteManageUserService;

    @Autowired
    private RemoteOrderService remoteOrderService;

    @Autowired
    private ReviewImageMapper reviewImageMapper;

    @Autowired
    private TagMapper tagMapper;


    @Override
    public List<UserReview> getPageList(UserReview userReview) {
        List<UserReview> userReviews = baseMapper.selectList(new LambdaQueryWrapper<UserReview>().eq(userReview.getRating() != null, UserReview::getRating, userReview.getRating()));
        if(!CollectionUtils.isEmpty(userReviews)){
            for (UserReview review : userReviews) {
                R<User> result = remoteManageUserService.getUserById(review.getUserId(), SecurityConstants.INNER);
               if(R.FAIL == result.getCode()){
                   throw new ServiceException(result.getMsg());
                }
                User user = result.getData();
                //设置用户昵称
                review.setUserNickName(user.getNickname());
                R<OrderInfo> orderInfo = remoteOrderService.getOrderInfoById(review.getOrderId(), SecurityConstants.INNER);
                if(R.FAIL == orderInfo.getCode()){
                    throw new ServiceException(orderInfo.getMsg());
                }
                review.setServiceOptionName(orderInfo.getData().getServiceOptionName());
            }
        }
        return userReviews;
    }

    @Override
    public UserReview getUserReviewDetailById(Integer id) {
        //获取用户评价
        UserReview userReview = baseMapper.selectById(id);
        if(userReview != null){
            R<User> result = remoteManageUserService.getUserById(userReview.getUserId(), SecurityConstants.INNER);
            if(result != null){
                User user = result.getData();
                //设置用户昵称
                userReview.setUserNickName(user.getNickname());
            }
            R<OrderInfo> orderInfo = remoteOrderService.getOrderInfoById(userReview.getOrderId(), SecurityConstants.INNER);
            if (orderInfo != null){
                userReview.setServiceOptionName(orderInfo.getData().getServiceOptionName());
            }
        }
        if(userReview.getHasImages() == 1){
            //根据用户评价id获取用户评价图片
            List<ReviewImage> reviewImages = reviewImageMapper.selectList(new LambdaQueryWrapper<ReviewImage>().eq(ReviewImage::getReviewId, id));
            if(!CollectionUtils.isEmpty(reviewImages)){
                userReview.setReviewImages(reviewImages);
            }
        }
        return userReview;
    }

    @Override
    public UserReviewVo getUserReviewList(UserReviewDto userReviewDto) {
        //获取服务详情id
        List<UserReview> userReviewList = baseMapper.getUserReviewList(userReviewDto);
        if(!CollectionUtils.isEmpty(userReviewList)){
            for (UserReview userReview : userReviewList) {
                R<User> result = remoteManageUserService.getUserById(userReview.getUserId(), SecurityConstants.INNER);
                if(R.FAIL == result.getCode()){
                    throw new ServiceException(result.getMsg());
                }
                User user = result.getData();
                userReview.setUserNickName(user.getNickname());
                userReview.setAvatarUrl(user.getAvatarUrl());
                if(userReview.getHasImages() == 1){
                    //根据用户评价id获取用户评价图片
                    List<ReviewImage> reviewImages = reviewImageMapper.selectList(new LambdaQueryWrapper<ReviewImage>().eq(ReviewImage::getReviewId, userReview.getId()));
                    if(!CollectionUtils.isEmpty(reviewImages)){
                        userReview.setReviewImages(reviewImages);
                    }
                }
            }
        }
        //查询所有的评价标签
        List<Tag> tagList = tagMapper.selectList(null);
        //创建UserReviewVo对象
        UserReviewVo userReviewVo = new UserReviewVo();
        userReviewVo.setTagList(tagList);
        userReviewVo.setUserReviewList(userReviewList);
        return userReviewVo;
    }
}




